#include "fig_boss_u14.h"
#include "fig_bullet.h"
#include "fig_enemybullet_master.h"
#include "fig_life_obj.h"
#include "fig_functions.h"

#include "num/rand.h"


FIGBossU14::LaserHand::LaserHand(
								 FIGBonePart* pArm,
								 FIGBonePart* pMidHand,
								 FIGBonePart* pLaserBone,
								 float		 ArmClockWiseLimit,
								 float		 ArmAntiClockWiseLimit,
								 float		 ForeArmClockWiseLimit,
								 float		 ForeArmAntiClockWiseLimit
								 ) :
									m_pArm(pArm),
									m_pMidHand(pMidHand),
									m_pLaserBone(pLaserBone),
									m_ArmClockWiseLimit(ArmClockWiseLimit),
									m_ArmAntiClockWiseLimit(ArmAntiClockWiseLimit),
									m_ForeArmClockWiseLimit(ForeArmClockWiseLimit),
									m_ForeArmAntiClockWiseLimit(ForeArmAntiClockWiseLimit),
									m_MidHandMovement(MOVEMENT_NONE),
									m_MidHandRotateSpeed(0),
									m_ArmMovement(MOVEMENT_NONE),
									m_ArmRotateSpeed(0)									
									
									
{
	m_ArmMovement = MOVEMENT_NONE;
	m_ArmRotateCounter.SetLimit(0.75f);
	m_ArmRotateCounter.ForceReady();

	m_MidHandMovement = MOVEMENT_NONE;
	m_MidHandRotateCounter.SetLimit(0.75f);
	m_MidHandRotateCounter.ForceReady();

	
}


void	FIGBossU14::LaserHand::Tick(float dt)
{

	float Rotation;   	
	math::Vec2 P;
	// Arm Rotation ------------------------------------------

	/*
	m_ArmRotateCounter.Tick(dt);
	if(m_ArmRotateCounter.IsReady())
	{
		
		m_ArmMovement = (BONE_MOVEMENT)num::RAND32::Instance().RandInt(
																		(int)MOVEMENT_NONE, 
																		(int)MOVEMENT_C -1
																		);

		
		m_ArmRotateSpeed = num::RAND32::Instance().RandFloat(5.0f,10.0f);
		m_ArmRotateCounter.Reset();
	}

	
	switch(m_ArmMovement)
	{
		case  MOVEMENT_NONE:
		break;

		case MOVEMENT_CLOCKWISE:
		{
			const float OrignalRoation = m_pArm->GetRotationBackup();
			BackupRotaion = Rotation = m_pArm->GetRotation();
			Rotation += m_ArmRotateSpeed * dt;

			if( Rotation > 15.0f + OrignalRoation)
			{
				m_ArmRotateCounter.ForceReady();
			}
			else
			{
				if( Rotation  > 360.0f)
					Rotation = 359.0f;
				m_pArm->SetRotation(Rotation);
			}
		}

		break;

		case MOVEMENT_ANTICLOCKWISE:
		{
			const float OrignalRoation = m_pArm->GetRotationBackup();
			Rotation = m_pArm->GetRotation();
			Rotation -= m_ArmRotateSpeed * dt;
			

			if(Rotation < -15.0f + OrignalRoation )
			{  				
				m_ArmRotateCounter.ForceReady();
			}
			else
			{

				if(Rotation < 0.0f)
					Rotation = Rotation + 360.0f; 

				m_pArm->SetRotation(Rotation);
			}
		}
		break;

		default:
		break;

	}
	
	// Relocate ForeArm -----------------------------------------------------

	Rotation = m_pArm->GetRotation() - m_pArm->GetRotationBackup();
	
	P = FigFunctions::RotateAroundPos( 
										m_pArm->GetPosition(),
										m_pMidHand->GetPosBackup(),
										Rotation,
										true
										);

	m_pMidHand->SetPosition(P);
	 */
	
	// ForeArm Rotation ----------------------------------------------------
	
	m_MidHandRotateCounter.Tick(dt);
	if(m_MidHandRotateCounter.IsReady())
	{
		
		m_MidHandMovement =	
								(BONE_MOVEMENT)num::RAND32::Instance().RandInt(
																		(int)MOVEMENT_NONE, 
																		(int)MOVEMENT_C -1
																		);

		
		m_MidHandRotateSpeed = num::RAND32::Instance().RandFloat(5.0f,10.0f);
		m_MidHandRotateCounter.Reset();
	}

	switch(m_MidHandMovement)
	{
		case  MOVEMENT_NONE:
		break;

		case MOVEMENT_CLOCKWISE:
		{
			const float OrignalRoation = m_pMidHand->GetRotationBackup();
			Rotation = m_pMidHand->GetRotation();
			Rotation += m_MidHandRotateSpeed * dt;
			
			float CapDegree = OrignalRoation + m_ForeArmClockWiseLimit;
			
			if(  Rotation > CapDegree || Rotation >= 360.0f   )
			{
				m_MidHandRotateCounter.ForceReady();
			}
			else
			{  
				if(Rotation >= 360.0f)
					Rotation = 359.0f; 
				m_pMidHand->SetRotation(Rotation);
			}
		}
		break;

		case MOVEMENT_ANTICLOCKWISE:
		{
			const float OrignalRoation = m_pMidHand->GetRotationBackup();
			Rotation = m_pMidHand->GetRotation();
			Rotation -= m_MidHandRotateSpeed * dt;
			
			if(Rotation < OrignalRoation - m_ForeArmAntiClockWiseLimit)
			{
				m_MidHandRotateCounter.ForceReady();
			}
			else
			{

				m_pMidHand->SetRotation(Rotation);
			}
		}
		break;

		default:
		break;

	}
   

	//Relocate Laser ------------------------------

	Rotation = m_pMidHand->GetRotation() - m_pMidHand->GetRotationBackup();
	
	P = FigFunctions::RotateAroundPos( 
										m_pMidHand->GetPosition(),
										m_pLaserBone->GetPosBackup(),
										Rotation,
										true
										);

	m_pLaserBone->SetPosition(P);
   


	
}

math::Vec2		FIGBossU14::LaserHand::GetLaserShootPoint(const math::Vec2& Origianl_WP) const
{
	return Origianl_WP +  (m_pLaserBone->GetPosition() - m_pLaserBone->GetPosBackup());
}